package com.myf.warehouse3d.controllers;

import com.myf.ApiResult;
import com.myf.Utility;
import com.myf.dtos.warehouse.*;
import com.myf.features.BaseController;
import com.myf.services.WarehouseLocationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/api/warehouse/location")
@Tag(name = "库位功能", description = "库位相关API接口")
public class WarehouseLocationController extends BaseController {
    private final WarehouseLocationService warehouseLocationService;

    public WarehouseLocationController(WarehouseLocationService warehouseLocationService) {
        this.warehouseLocationService = warehouseLocationService;
    }

    @Operation(summary = "新建或保存库位接口")
    @PutMapping("save")
    public ApiResult save(@RequestBody WarehouseLocationInput input) {
        try {
            return ApiResult.createSuccess(warehouseLocationService.save(input, getCurrentUser().id));
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "分页查询库位列表接口")
    @PostMapping("query")
    public ApiResult query(@RequestBody WarehouseLocationQueryInput input) {
        try {
            return ApiResult.createSuccess(warehouseLocationService.query(input));
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "通过库位id获取详细信息接口")
    @GetMapping("{id}")
    public ApiResult get(@PathVariable int id) {
        try {
            var data = warehouseLocationService.getById(id);
            return ApiResult.createSuccess(data);
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "根据库位id集合批量删除库位接口")
    @DeleteMapping()
    public ApiResult delete(@RequestParam String ids) {
        try {
            var toDeleteIds = new ArrayList<Integer>();
            var temp = ids.split(",");
            for (String id : temp) {
                var res = Utility.tryParse(id);
                if (res != null) {
                    toDeleteIds.add(res);
                }
            }
            if (toDeleteIds.isEmpty()) {
                return ApiResult.createSuccess(0);
            }
            return ApiResult.createSuccess(warehouseLocationService.delete(toDeleteIds, getCurrentUser().id));
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }

    @Operation(summary = "根据仓库id获取库位下拉列表框中数据的接口")
    @GetMapping("items")
    public ApiResult getForSelectItems(@RequestParam(required = false) String keyword, @RequestParam(required = false) Integer warehouseId) {
        try {
            return ApiResult.createSuccess(warehouseLocationService.getForSelectItems(keyword, warehouseId));
        } catch (Exception e) {
            return ApiResult.createError(e.getMessage());
        }
    }
}
